package com.geek.ch06.anno;

import com.geek.entity.User;
import com.geek.vo.UserQueryVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface UserMapper {

    @Select("select * from user where name like concat('%',#{name},'%')")
    public List<User> selectUserByName(String name);

    @Options(useGeneratedKeys = true , keyProperty = "id" , keyColumn = "id")
    @Insert("insert into user values(null , #{name},#{age} , #{gender} ,#{birthday} ,null, #{pwd} , 0)")
    public int insertUser(User user);

    @Update("update user set state=#{state} where name=#{name}")
    public int updateUser(@Param("state") String state , @Param("name") String name);

    @Delete("delete from user where id = #{id}")
    public int deleteUser(int id);

    @Select(
            "<script>" +
                    "select * from user "+
                    "<where>" +
                        "<if test='name!=null and name!=\"\"'>"+
                            "and name like concat('%',#{name},'%') "+
                        "</if>"+
                        "<if test='state!=null and state!=\"\"'>"+
                            "and state = #{state} "+
                        "</if>"+
                    "</where>"+
            "</script>"
    )
    public List<User> selectUsers(@Param("state") String state , @Param("name") String name);

    //<!-- 注解方式作业1：查询订单信息及其下单者信息  OO  -->
    @Select("select * from user where id = #{userId}")
    User selectUser(Integer userId);

    //<!-- 注解方式作业3：性别和姓名非空查询，且id in(10,20,30)  script脚本 -->
    @Select({"<script>",
            "select * from user",
            "<where>",
            "<if test = 'user.gender != null and user.gender != \"\"'>",
            "and gender = #{user.gender}",
            "</if>",
            "<if test = 'user.name != null and user.name != \"\"'>",
            "and name like concat('%',#{user.name},'%')",
            "</if>",
            "<if test = 'ids != null '>",
            "<foreach collection='ids' item='id' open='and id in(' close=')' separator=','>",
            "#{id}",
            "</foreach>",
            "</if>",
            "</where>",
            "</script>"})
    List<User> getUsersByGenderAndNameAndId(UserQueryVO userQueryVO);

}
